#include "Server.h"

Server::Server(boost::asio::io_context &ioc, short port) : m_ioc{ ioc }, m_actor{ ioc, tcp::endpoint(tcp::v4(), port)}
{
	start_accept();
}

void Server::cleanSession(std::string uuid)
{
	m_session_map.erase(uuid);
}

void Server::start_accept()
{
	auto new_session = std::make_shared<Session>(m_ioc, this);
	//Session* new_session = new Session(m_ioc);
	m_actor.async_accept(new_session->Socket(), bind(&Server::handle_accept, this, new_session, std::placeholders::_1));
}

void Server::handle_accept(std::shared_ptr<Session> new_session, const boost::system::error_code& ec)
{
	if (!ec) {
		new_session->Start();
		m_session_map.insert({ new_session->GetUuid(), new_session });
	}
	else {
		//delete new_session;
		std::cout << ec.message() << std::endl;
	}
	start_accept();
}
